var ns;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            type: 1,
            selecttype: 1,
            parentid: 0,
            toplevel: 0,
            fromtype: 0
        }
    },
    methods: {
        randomSwitchBtn: function(index, reload) {
            var top_tab = $api.byId('tab');
            var topAct = $api.dom(top_tab, '.aui-tab-item.aui-active');
            $api.removeCls(topAct, 'aui-active');
            var obj = $api.byId('tabbar' + index);
            $api.addCls(obj, 'aui-active');
            api.setFrameGroupIndex({
                name: 'top_tab_switch',
                index: index,
                scroll: true,
                reload: reload
            });
            if (app.form.parentid > 0) {
                setTimeout(function() {
                    api.sendEvent({
                        name: 'do_send_project_parentid',
                        extra: {
                            parentid: app.form.parentid,
                        }
                    });
                }, 500)
            }
        },
        init_swithtab: function(defaultindex) {
            var top_tab = 　$api.byId('tab')
            var body_h = api.winHeight;
            var top_tab_h = $api.offset(top_tab).h;
            var header_h = api.pageParam.header_h;
            api.openFrameGroup({
                useWKWebView: true,
                name: 'top_tab_switch',
                scrollEnabled: true,
                rect: {
                    x: 0,
                    y: top_tab_h + header_h,
                    w: 'auto',
                    h: body_h - top_tab_h - header_h
                },
                index: defaultindex,
                preload: 0,
                frames: [{
                    name: 'choosemyproject_frm',
                    url: 'choosemyproject_frm.html',
                    bounces: false,
                    pageParam: {
                        type: app.form.type,
                        selecttype: app.form.selecttype,
                        toplevel: app.form.toplevel,
                        fromtype: app.form.fromtype,
                    }
                }, {
                    name: 'chooseprojectlist_frm',
                    url: 'chooseprojectlist_frm.html',
                    bounces: false,
                    pageParam: {
                        type: app.form.type,
                        selecttype: app.form.selecttype,
                        parentid: app.form.parentid,
                        toplevel: app.form.toplevel,
                        fromtype: app.form.fromtype,
                    }
                }]
            }, function(ret, err) {
                var top_tab = $api.byId('tab');
                var tabAct = $api.dom(top_tab, '.aui-tab-item.aui-active');
                $api.removeCls(tabAct, 'aui-active');
                var name = ret.name;
                var index = ret.index;
                $api.addCls($api.byId('tabbar' + index), 'aui-active');
            })
        }
    }
});

apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.selecttype = api.pageParam.selecttype || 1;
    app.form.toplevel = api.pageParam.toplevel || 0;
    app.form.parentid = api.pageParam.parentid || 0;
    app.form.fromtype = api.pageParam.fromtype || 0;
    var defaultindex = 0;
    if (app.form.parentid > 0) {
        defaultindex = 1;
    }
    app.init_swithtab(defaultindex);
    api.addEventListener({
        name: 'do_switch_projecttab'
    }, function(ret, err) {
        app.form.parentid = ret.value.parentid;
        app.randomSwitchBtn(ret.value.tabindex, false);
    });
    api.addEventListener({
        name: 'closechooseproject'
    }, function(ret, err) {
        setTimeout(function() {
            api.closeWin();
        }, 1000)
    });

    api.addEventListener({
        name: 'closecpframe'
    }, function (ret, err) {
        api.closeWin({
            name: 'chooseprojectlist_frm'
        })
    });
}
